package org.hzy.dao.impl;

import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.sql.Date;
import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

public abstract class BaseDao<T>{
	
	private Class entityclass;
	
	private SessionFactory sf;
	
	public Class getEntityclass() {
		return entityclass;
	}
	public void setEntityclass(Class entityclass) {
		this.entityclass = entityclass;
	}
	public SessionFactory getSf() {
		return sf;
	}
	public void setSf(SessionFactory sf) {
		this.sf = sf;
	}
	public BaseDao() {
		super();
		this.entityclass =getParameterizedType(this.getClass());
	}
	 protected Class getParameterizedType(Class clazz) {   
	        Type ty = clazz.getGenericSuperclass();  

	        Type[] types=null;  
	        if (ty instanceof ParameterizedType) {  
	            types= ((ParameterizedType) ty).getActualTypeArguments();  
	        }else{    
	            try {    
	                throw new Exception("not find ParameterizedType!");    
	            } catch (Exception e) {    
	                e.printStackTrace();    
	            }  
	        }  
	        return (Class) types[0];  
	    }  
	
	 public Session getSession(){
		 return this.getSf().getCurrentSession();
	 }
	 
	 
    public T get_object(Integer id) {  
        return (T)this.getSession().get(entityclass, id);
    } 
	
    public void create(T t){
//    	Session sess=getHibernateTemplate().getSessionFactory().getCurrentSession();
//    	Transaction tran=sess.beginTransaction();
//    	sess.save(t);
    	this.getSession().save(t);
    }
    
    public List<T> get_All(){
//    	Session sess=getHibernateTemplate().getSessionFactory().getCurrentSession();
//    	Transaction tran=sess.beginTransaction();
//    	String classname=this.entityclass.getName();
//    	Date aa=new Date(new java.util.Date().getTime());
//    	Query query=sess.createQuery("From "+classname+" v where v.VTime>?");
//    	query.setDate(0, aa);
//    	List list=query.list();
//    	return list;
    	return null;
    }   
}
